package com.apes.scm.voucher.repository;

import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.account.account.model.InventoryBatch;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.voucher.model.Voucher;
import com.apes.scm.voucher.model.VoucherInventory;
import com.apes.scm.voucher.model.VoucherItem;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @author xul
 * @create 2018-01-23 16:07
 */
public interface VoucherInventoryRepository extends CustomRepository<VoucherInventory, Long> {

    List<VoucherInventory> findByVoucherItem(VoucherItem voucherItem);
    List<VoucherInventory> findByVoucher(Voucher voucher);

    @Query("select e from VoucherInventory e " +
            " where e.company = :company " +
            "   and e.partner = :partner " +
            "   and e.product = :product " +
            "   and e.voucher.voucherType = 'CUSTOMER_SALE' " +
            "   and e.voucher.returned = false " +
            "   and e.productQty - e.qtyReturned > 0 " +
            " order by e.inventoryBatch.cooperation.priority desc, e.inventoryBatch.batchNumber desc, e.inventoryBatch.batchSerial desc")
    Page<VoucherInventory> findInventoryBatchForSaleReturn(@Param("company") PartyRole company, @Param("partner") PartyRole partner, @Param("product") Product product, Pageable pageable);

    @Query("select e from VoucherInventory e " +
            " where e.company = :company " +
            "   and e.partner = :partner " +
            "   and e.product = :product " +
            "   and e.inventorySupplier = :supplier " +
            "   and e.voucher.voucherType = 'CUSTOMER_SALE' " +
            "   and e.voucher.returned = false " +
            "   and e.productQty - e.qtyReturned > 0 " +
            " order by e.inventoryBatch.cooperation.priority desc, e.inventoryBatch.batchNumber desc, e.inventoryBatch.batchSerial desc")
    Page<VoucherInventory> findInventoryBatchForSaleReturn(@Param("company") PartyRole company, @Param("partner") PartyRole partner,
                                                           @Param("supplier") PartyRole supplier, @Param("product") Product product,
                                                           Pageable pageable);


    @Query("select e from VoucherInventory e " +
            " where e.company = :company " +
            "   and e.partner = :partner " +
            "   and e.product = :product " +
            "   and e.voucher.voucherType = 'CUSTOMER_SALE' " +
            "   and e.voucher.origin = :origin " +
            "   and e.voucher.returned = false " +
            "   and e.productQty - e.qtyReturned > 0 " +
            " order by e.inventoryBatch.cooperation.priority desc, e.inventoryBatch.batchNumber desc, e.inventoryBatch.batchSerial desc")
    Page<VoucherInventory> findInventoryBatchForMultiSaleReturn(@Param("company") PartyRole company, @Param("partner") PartyRole partner,
                                                                @Param("product") Product product, @Param("origin") String origin, Pageable pageable);
}
